//
//  BanBu_VideoView.m
//  BanBu
//
//  Created by Wang Xj on 13-8-30.
//
//

#import "BanBu_VideoView.h"
#import <MediaPlayer/MediaPlayer.h>
#import "AppDataManager.h"
#import "AppCommunicationManager.h"

@implementation BanBu_VideoView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag=101;
        button.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        [button addTarget:self action:@selector(playVideo) forControlEvents:UIControlEventTouchUpInside];
        UIImage * image = [UIImage imageNamed:@"btn_msg_video_11.png"];
        
        [button setBackgroundImage:image forState:UIControlStateNormal];
        self.playButton = button;
        
        [self addSubview:button];
        
        self.playImageView = [[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 64)] autorelease];
        self.playImageView.image = [UIImage imageNamed:@"palyBtn.png"];
        [self addSubview:self.playImageView];
        
        _status = 6;
        
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"file-loading.png"]];
        
        self.hintLabel = [[[UILabel alloc]initWithFrame:CGRectZero] autorelease];
        self.hintLabel.backgroundColor = [UIColor clearColor];
        self.hintLabel.textAlignment = UITextAlignmentCenter;
        [self addSubview:self.hintLabel];
        self.hintLabel.font = [UIFont systemFontOfSize:12];
    }
    return self;
}

//设置视频路径
- (void)setVideo:(NSString *)videoStr
{
    self.videoPath =[NSString stringWithFormat:@"%@.mp4",[AppComManager pathForVideoMsgMedia:videoStr]];
}

//设置缩略图
- (void)setThumbnail:(NSString *)thumbnail
{
    self.hintLabel.hidden = YES;
    UIImage * image = nil;
    
    if (self.snapMsg)
    {
        image = [UIImage imageNamed:@"photo_default.png"];
        self.playButton.userInteractionEnabled = NO;
        
        self.hintLabel.frame = CGRectMake(self.center.x-25, 0, 50, 28);
        self.hintLabel.text = NSLocalizedString(@"longPressToSee", nil);
        self.hintLabel.hidden = NO;
    }
    else
    {
        self.playButton.userInteractionEnabled = YES;
        if ([thumbnail pathExtension])
        {
            image =[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@.jpg",[AppComManager pathForVideoMsgMedia:thumbnail]]];
        }
        else
        {
            image =[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@.jpg",[AppComManager pathForVideoMsgMedia:thumbnail]]];
        }
    }
    
    [self.playButton setBackgroundImage:image forState:UIControlStateNormal];
}

-(void)setStatus:(MediaStatus)status
{
    if(_status != status)
    {
        _status = status;
        if(_status != MediaStatusNormal)
        {
            self.playButton.enabled = NO;
            self.playImageView.hidden = YES;
        }
        else
        {
            self.playButton.enabled = YES;
            self.playImageView.hidden = NO;
        }
    }
    [self layoutSubviews];
}

//点击播放按钮；
-(void)playVideo
{
    MPMoviePlayerViewController * movieController= [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:self.videoPath]];
    
    [self.chatController presentMoviePlayerViewControllerAnimated:movieController];
    [movieController release];
}

-(void)layoutSubviews
{
    self.playButton.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.playImageView.center = self.playButton.center;
    self.hintLabel.frame = CGRectMake(self.playButton.center.x-25, 0, 50, 28);
}

-(void)dealloc
{
    [_hintLabel release],_hintLabel = nil;
    [_playButton release],_playButton = nil;
    [super dealloc];
}

@end
